package server.mapper;

import servlet.Request;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by louisyuu on 2020/4/2 上午11:47
 */
public class Host {


    public final static Map<String, Host> hostMap = new HashMap<>();

    private String host;

    private String appBase;

    /**
     * /Users/lewis/JavaWorkspace/study/Secondary/tomcat/mine/my-minicat-demo/minicat/src/main/resources/webapps
     */
    private String appBaseAbsolutePath;

    private List<Context> contexts;


    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getAppBase() {
        return appBase;
    }

    public void setAppBase(String appBase) {
        this.appBase = appBase;
    }


    public Host() {
    }

    public Host(String host, String appBase, List<Context> contexts) {
        this.host = host;
        this.appBase = appBase;
        this.contexts = contexts;
    }

    public List<Context> getContexts() {
        return contexts;
    }

    public void setContexts(List<Context> contexts) {
        this.contexts = contexts;
    }


    public String getAppBaseAbsolutePath() {
        return appBaseAbsolutePath;
    }

    public void setAppBaseAbsolutePath(String appBaseAbsolutePath) {
        this.appBaseAbsolutePath = appBaseAbsolutePath;
    }

    public static Context getContext(Request request) {

        Host localhost = hostMap.get("localhost");//暂时默认为localhost


        List<Context> contexts = localhost.getContexts();

        for (Context context : contexts) {

            String[] split = request.getUrl().split("/");

            System.out.println("split url " + split);
            if (context.equals(split[0])) {
                return context;
            }


        }

        return null;


    }
}
